package com.xysd.express.wxapi;

import android.content.Context;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xysd.express.util.LoadingDialogUtils;
import com.xysd.express.util.ToastUtil;

public class BaseWeinxinPay {
    Context context;

    public BaseWeinxinPay(Context context) {
        this.context = context;
    }

    PayReq req;
    IWXAPI msgApi;

    public void gotoWeixinPay(WexinPayInfoObj.WexinPayInfo wexinPayInfo) {
        req = new PayReq();
        msgApi = WXAPIFactory.createWXAPI(context, null);
        req.appId = wexinPayInfo.appid;
        msgApi.registerApp(req.appId);//将app注册到微信

        req.partnerId = wexinPayInfo.mch_id;
        req.prepayId = wexinPayInfo.prepay_id;
        req.packageValue = wexinPayInfo.packages;
        req.nonceStr = wexinPayInfo.nonce_str;
        req.timeStamp = wexinPayInfo.orderDate;
        req.sign = wexinPayInfo.sign;
        if (!msgApi.isWXAppInstalled()) {
            ToastUtil.show("请安装微信客户端后重试");
            LoadingDialogUtils.closeLoadingDialog();
            return;
        }
        if (!msgApi.isWXAppSupportAPI()) {
            ToastUtil.show("版本不能支付");
            LoadingDialogUtils.closeLoadingDialog();
            return;
        }
        msgApi.sendReq(req);
    }

    /**
     * 根据自定义参数类封装调微信支付方法
     * @param wexinPayInfo
     */
    public void gotoWeixinPayMine(WeixinPayInfoObjMine.WeixinpayParams wexinPayInfo){
        req = new PayReq();
        msgApi = WXAPIFactory.createWXAPI(context, null);
        req.appId = wexinPayInfo.appid;
        msgApi.registerApp(req.appId);//将app注册到微信

        req.partnerId = wexinPayInfo.partnerid;
        req.prepayId = wexinPayInfo.prepayid;
        req.packageValue = wexinPayInfo.packages;
        req.nonceStr = wexinPayInfo.noncestr;
        req.timeStamp = wexinPayInfo.timestamp+"";
        req.sign = wexinPayInfo.sign;
        if (!msgApi.isWXAppInstalled()) {
            ToastUtil.show("请安装微信客户端后重试");
            LoadingDialogUtils.closeLoadingDialog();
            return;
        }
        if (!msgApi.isWXAppSupportAPI()) {
            ToastUtil.show("版本不能支付");
            LoadingDialogUtils.closeLoadingDialog();
            return;
        }
        msgApi.sendReq(req);
    }
}
